package com.beiding.v2.grammar;

import com.beiding.v2.base.IUnit;
import com.beiding.v2.element.IField;
import com.beiding.v2.element.ISymbol;
import com.beiding.v2.type.KeywordType;
import com.beiding.v2.type.LogicSymbolType;
import com.beiding.v2.type.SymbolType;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class QueryUtils {


    public static void dealLogicSymbolMiss(List<IUnit> conditionUnits, LogicSymbolType mt) {

        if (conditionUnits.size() == 1) {
            return;
        }

        //开始从前向后处理
        for (int i = 0; i < conditionUnits.size() - 1; i++) {

            //当前的
            IUnit current = conditionUnits.get(i);

            boolean currentIsLogicSymbol = false;

            if (current instanceof ISymbol) {
                currentIsLogicSymbol = ((ISymbol) current).getSymbolType() instanceof LogicSymbolType;
            }

            if (!currentIsLogicSymbol) {
                IUnit next = conditionUnits.get(i + 1);
                boolean nextIsLogicSymbol = false;
                if (next instanceof ISymbol) {
                    nextIsLogicSymbol = ((ISymbol) next).getSymbolType() instanceof LogicSymbolType;
                }

                if (!nextIsLogicSymbol) {
                    conditionUnits.add(i + 1, new Symbol(mt));
                    //跳过下一个
                    i++;
                }
            }

        }
    }

    public static IUnit auto(Object obj) {

        if (obj == null) {
            return new Value(null);
        }

        if (obj instanceof IUnit) {
            return (IUnit) obj;
        }
        if (obj instanceof IBuilder) {
            return ((IBuilder) obj).createExpression();
        } else if (obj instanceof IField) {
            return new FieldCapture((IField) obj);
        } else if (obj instanceof KeywordType) {
            return new Keyword((KeywordType) obj);
        } else if (obj instanceof SymbolType) {
            return new Symbol((SymbolType) obj);
        } else {
            return new Value(obj);
        }

    }
}
